"use strict";

import mongoose from "mongoose";
import config from "config-lite";
import chalk from "chalk";
// mongoose.connect('mongodb://elm:62238623f2@193.123.228.251:27017/elm', {useMongoClient:true});
mongoose.connect("mongodb://193.123.228.251:27017/elm", {
  // auth: { authSource: "admin" },
  // authSource: 'admin',
  // user: "root",
  // pass: "zRqUjGqLFeJFtbUZ",
  // useMongoClient: true,
  user: "elm",
  pass: "62238623f2",
  useNewUrlParser: true,
  keepAlive: true,
});
mongoose.Promise = global.Promise;

const db = mongoose.connection;

db.once("open", () => {
  console.log(chalk.green("线上mongodb连接数据库成功"));
});

db.on("error", function (error) {
  console.error(chalk.red("Error in MongoDb connection: " + error));
  mongoose.disconnect();
});

db.on("close", function () {
  console.log(chalk.red("数据库断开，重新连接数据库"));
  mongoose.connect("mongodb://elm:62238623f2@193.123.228.251:27017/elm", {
    server: { auto_reconnect: true },
  });
  // mongoose.connect(
  //   "mongodb://root:zRqUjGqLFeJFtbUZ@193.123.228.251:27017/admin?authSource=admin",
  //   { server: { auto_reconnect: true } }
  // );
});

export default db;
